/*
 * Author: vyouzhi <vyouzhi@163.com>
 * http://www.xda.cn
 *
 * File: libevent_lib.c
 * Create Date: 2011-09-22 09:29:21
 *
 */

#include "thread_lib.h"

void on_accept(int fd, short ev, void *arg){
	int client_fd;
	struct sockaddr_in client_addr;
	socklen_t client_len = sizeof(client_addr);
	struct client *client;

	/* Accept the new connection. */
	client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len);
	if (client_fd == -1) {
		warn("accept failed");
		return;
	}

	/* Set the client socket to non-blocking mode. */
	if (SetNonBlocking(client_fd) < 0)
		warn("failed to set client socket non-blocking");

	CQ_ITEM *item = cqi_new();
    int tid = (last_thread + 1) % settings.num_threads;

    LIBEVENT_THREAD *thread = threads + tid;

    last_thread = tid;

    item->sfd = client_fd;

    cq_push(thread->new_conn_queue, item);

    if (write(thread->notify_write_fd, "", 1) != 1) {
        perror("Writing to thread notify pipe");
    }
}

/* vim: set ts=4 sw=4: */

